<!--
 * @Author: 一品网络技术有限公司
 * @Date: 2022-07-01 08:36:59
 * @LastEditTime: 2022-09-17 12:46:30
 * @FilePath: \ypcmsvue3\src\pages\classify\add.vue
 * @Description:
 * 联系QQ:58055648
 * Copyright (c) 2022 by 东海县一品网络技术有限公司, All Rights Reserved.
-->
<template>
  <yplayout v-if="isLoad">
    <template #header="head">
      <div class="flex items-center">
          <i class="ri-2x text-gray-500 ml-2 ri-play-list-add-line"></i>
          <div class="text-xl px-3">无限分类</div>
          <div class="text-gray-400">{{ formData.pid?'增加子级':'增加' }}</div>
      </div>
      <div class="flex-1"></div>
    </template>
    <template #list>

      <div class="text-sm bg-white w-full">
        <div>
          <div class="flex pb-2 items-center">
            <div class="w-36 text-right pr-5">所属分类：</div>
            <div class="flex-1 leading-8 text-blue-600 font-bold">
              {{parent}}
            </div>
          </div>
          <div class="flex items-center">
            <div class="w-36 text-right pr-5">分类名称：</div>
            <div class="flex-1" id="title">
              <yptextarea v-model="formData.title" placeholder="一行一个"></yptextarea>
            </div>
          </div>
        </div>
        <div class="flex justify-center py-3 space-x-2">
          <div>
            <button class="btn btn-hong" type="button" @click="save">提交</button>
          </div>
          <div>
            <router-link to="list" class="btn">取消</router-link>
          </div>
        </div>
      </div>
    </template>
  </yplayout>




</template>
<script setup>
  import { router } from '@/router'
  import { getData,postData,alter } from '@/common/base'
  import { ref, reactive, onMounted } from 'vue'
  const isLoad=ref(false)
  const emits = defineEmits(['close','save'])
  const parent = ref('顶级')
  const formData = reactive({
    title: '',
    pid: router.currentRoute.value.query.pid
  })

  //增加分类
  const save=async ()=>{
    const resp= await postData("classify/save",formData)
    if(resp.code==1){
      alter({ type: 'alter-success', text: resp.msg })
      router.push('/classify/list')
    }
  }
  
  const getParent=async(pid)=>{
    const resp= await getData('classify/read',{id:pid})
    if(resp.code==1){
      parent.value=resp.data.title
    }
  }



  onMounted(async()=>{

    if(formData.pid){
     await getParent(formData.pid)
    }



    isLoad.value=true
  })
</script>
